package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.constants.AutoConstants;
import com.dss.auto_deploy.domain.AdmDockerDeploy;
import com.dss.auto_deploy.vo.AdmDockerDeployVo;
import com.dss.auto_deploy.dto.*;
import com.dss.auto_deploy.service.IAdmDockerDeployService;
import com.dss.auto_deploy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
* docker部署项目模块配置
* @Description:docker部署项目模块配置控制层
* @author dongshoushan
* @date 2022-09-22 14:38:57
*/
@RestController
@RequestMapping("/admDockerDeploy")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: docker部署项目模块配置")
public class AdmDockerDeployController {

    /**
    * docker部署项目模块配置Service
    */
    private final  IAdmDockerDeployService admDockerDeployService;
    /**
    * 新增docker部署项目模块配置
    * @param addDto
    * @return
    */
    @PostMapping
    @ApiOperation(value = "新增docker部署项目模块配置", notes="新增docker部署项目模块配置")
    public Result insert(@RequestBody @Valid AdmDockerDeployAddDto addDto) {
        admDockerDeployService.insert(addDto);
        return Result.success("新增成功");
    }
    @PostMapping("/addNode")
    @ApiOperation(value = "扩容新增节点", notes="扩容新增节点")
    public Result addNode(@RequestBody @Valid AdmDockerDeployAddDto dto) {
        admDockerDeployService.addNode(dto);
        return Result.success("新增成功");
    }
    @PostMapping("/addModule")
    @ApiOperation(value = "新增模块", notes="新增模块")
    public Result addModule(@RequestBody @Valid AdmDockerDeployAddDto dto) {
        admDockerDeployService.addModule(dto);
        return Result.success("新增成功");
    }

    /**
    * 修改docker部署项目模块配置
    * @param updateDto
    * @return
    */
    @PutMapping
    @ApiOperation(value = "修改docker部署项目模块配置", notes="修改docker部署项目模块配置")
    public Result update(@RequestBody @Valid AdmDockerDeployUpdateDto updateDto) {
        admDockerDeployService.update(updateDto);
        return Result.success("修改成功");
    }
    @PutMapping("/changeStatus")
    @ApiOperation(value = "启用停用", notes="启用停用")
    public Result changeStatus(@RequestBody @Valid AdmDockerDeployUpdateDto updateDto) {
        admDockerDeployService.changeStatus(updateDto);
        return Result.success("操作成功");
    }

    /**
    * 批量移除docker部署项目模块配置
    * @param ids
    * @return
    */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "移除docker部署项目模块配置", notes="移除docker部署项目模块配置")
    public Result deleteBatch(@PathVariable Long[] ids) {
        admDockerDeployService.deleteBatch(ids);
        return Result.success("移除成功");
    }
  
    @PostMapping("/updateTag")
    @ApiOperation(value = "更新版本号", notes="更新版本号")
    public Result updateTag(@RequestBody @Valid UpdateDockerTagDto updateDto) {
        admDockerDeployService.updateTag(updateDto);
        return Result.success();
    }
    @PostMapping("/restartService")
    @ApiOperation(value = "重启", notes="重启")
    public Result restartService(String ids) throws IOException {
        admDockerDeployService.restartService(ids);
        return Result.success("重启成功");
    }
    @GetMapping("/restartServiceByIpAndServer")
    @ApiOperation(value = "重启netconf", notes="重启netconf")
    public Result restartServiceByIpAndServer(@RequestParam(value = "ip")String ip,@RequestParam(value ="module")String module) throws IOException {
        admDockerDeployService.restartServiceByIpAndServer(ip,module);
        return Result.success("重启成功");
    }
    @PostMapping("/stopService")
    @ApiOperation(value = "停止", notes="停止")
    public Result stopService(String ids) {
        admDockerDeployService.stopService(ids);
        return Result.success("停止成功");
    }
    @PostMapping("/pauseService")
    @ApiOperation(value = "暂停", notes="暂停")
    public Result pauseService(String ids) {
        admDockerDeployService.pauseService(ids);
        return Result.success("暂停成功");
    }
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据id查询", notes="根据id查询")
    public Result getById(@PathVariable String id) {
        AdmDockerDeploy admDockerDeploy = admDockerDeployService.getById(id);
        return Result.success(admDockerDeploy);
    }
    @PostMapping("/versionUpdate")
    @ApiOperation(value = "调整更新为此版本", notes="调整更新为此版本")
    public Result versionUpdate(@RequestBody VersionUpdateDto dto) throws IOException {
        admDockerDeployService.versionUpdate(dto);
        return Result.success("版本切换成功");
    }
    @GetMapping("/getSuccess")
    @ApiOperation(value = "获取部署成功的", notes="获取部署成功的")
    public Result getSuccess() throws InterruptedException {
        List<AdmDockerDeployVo> list=  admDockerDeployService.getSuccess();
        return Result.success(list);
    }

    /**
    * 分页查询docker部署项目模块配置
    * @param pageDto
    * @return
    */
    @ApiOperation(value = "分页查询docker部署项目模块配置", notes="分页查询docker部署项目模块配置")
    @GetMapping("/page")
    public  Result page(AdmDockerDeployPageDto pageDto) throws InterruptedException {
        return Result.success(admDockerDeployService.list(pageDto));
    }

    @GetMapping("/getJdkVersion")
    @ApiOperation(value = "查询jdk版本", notes="查询jdk版本")
    public Result<List<JdkVersionDto>> getJdkVersion() {
        List<JdkVersionDto> jdkVersionList = AutoConstants.JDK_VERSION_LIST;
        return Result.success(jdkVersionList);
    }

}
